package com.jh.paymentchannel.util.milian.utils;

import com.alibaba.fastjson.JSONObject;
import com.jh.paymentchannel.pojo.MiLianQuickRegister;
import com.jh.paymentchannel.pojo.MiLianRegister;
import com.jh.paymentchannel.util.milian.bean.*;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


public class ParamsUtil {

	public static String getBaseMerchRegisterParams(MiLianReginsterBean miLianReginsterBean , String key) {

		//签名
		JSONObject params = new JSONObject(16, true);
		params.put("bankAccountNo", miLianReginsterBean.getBankAccountNo());
		params.put("debitRate", miLianReginsterBean.getDebitRate());
		params.put("handleType", miLianReginsterBean.getHandleType());
		params.put("merchName", miLianReginsterBean.getMerchName());
		params.put("orderId", miLianReginsterBean.getOrderId());
		params.put("parent", miLianReginsterBean.getParent());
		params.put("phoneNo", miLianReginsterBean.getPhoneNo());

		/*设置签名*/
		miLianReginsterBean.setSign(MD5Util.getSign(params, key));
		
		String returnParam = "orderId="+miLianReginsterBean.getOrderId()+
				"&parent="+miLianReginsterBean.getParent()+
				"&handleType="+miLianReginsterBean.getHandleType()+
				"&merchName="+miLianReginsterBean.getMerchName()+
				"&merchAbb="+miLianReginsterBean.getMerchAbb()+
				"&merchAddress="+miLianReginsterBean.getMerchAddress()+
				"&telNo="+miLianReginsterBean.getTelNo()+
				"&email="+miLianReginsterBean.getEmail()+
				"&bankAccountName="+miLianReginsterBean.getBankAccountName()+
				"&legalPersonIdcard="+miLianReginsterBean.getLegalPersonIdcard()+
				"&phoneNo="+miLianReginsterBean.getPhoneNo()+
				"&bankAccountNo="+miLianReginsterBean.getBankAccountNo()+
				"&settBankName="+miLianReginsterBean.getSettBankName()+
				"&bankChannelNo="+miLianReginsterBean.getBankChannelNo()+
				"&bankSubName="+miLianReginsterBean.getBankSubName()+
				"&bankProvince="+miLianReginsterBean.getBankProvince()+
				"&bankCity="+miLianReginsterBean.getBankCity()+
				"&bankCode="+miLianReginsterBean.getBankCode()+
				"&bankAbbr="+miLianReginsterBean.getBankAbbr()+
				"&debitRate="+miLianReginsterBean.getDebitRate()+
				"&countFeeT0="+miLianReginsterBean.getCountFeeT0()+
				"&futureMinAmount="+miLianReginsterBean.getFutureMinAmount()+
				"&debitCapAmount="+miLianReginsterBean.getDebitCapAmount()+
				"&sign="+miLianReginsterBean.getSign()+
				"&bankCardPhoto="+miLianReginsterBean.getBankCardPhoto()+
				"&idCardPhoto="+miLianReginsterBean.getIdCardPhoto()+
				"&idCardBackPhoto="+miLianReginsterBean.getIdCardBackPhoto()+
				"&category="+miLianReginsterBean.getCategory()
				;
		if (!"".equals(miLianReginsterBean.getMerchId()) && miLianReginsterBean.getMerchId() != null) {
			returnParam = returnParam+"&changeType="+miLianReginsterBean.getChangeType()+
					"&merchId="+miLianReginsterBean.getMerchId()
					;
		}
		return returnParam;
	}

	public static String GetBase64StrOfImg(String fileName) {
		if (fileName == null) {
			return null;
		}
		try {
			FileInputStream stream = new FileInputStream(new File(fileName));
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			for (int n; (n = stream.read(b)) != -1;) {
				out.write(b, 0, n);
			}
			stream.close();
			out.close();
			byte[] bytes = out.toByteArray();
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encodeBuffer(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public static String getQuickPayJNParams(MiLianPayBean miLianPayBean ,MiLianRegister miLianRegister){
	    miLianPayBean.setOrder_time(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));

		RequestMsg requestMsg = new RequestMsg();
        requestMsg.setORDER_ID(miLianPayBean.getOrder_id());
        requestMsg.setTRANS_AMT1(miLianPayBean.getOrder_amt());
        requestMsg.setTRANS_ATIME(miLianPayBean.getOrder_time());
        requestMsg.setPAY_TYPE(miLianPayBean.getPay_type());
        requestMsg.setUSER_TYPE(miLianPayBean.getUser_type());
        requestMsg.setUSER_ID(miLianPayBean.getUser_id());
        requestMsg.setMSG_TYPE(miLianPayBean.getBus_code());
        requestMsg.setSIGN_TYPE(miLianPayBean.getSign_type());

		/*设置签名*/
		SignUtil signUtil = new SignUtil();
        miLianPayBean.setSign(signUtil.makePayByOtherSign(requestMsg , miLianRegister));

		String param ="ORDER_ID="+miLianPayBean.getOrder_id()+
				"&ORDER_AMT="+miLianPayBean.getOrder_amt()+
				"&ORDER_TIME="+miLianPayBean.getOrder_time()+
				"&PAY_TYPE="+miLianPayBean.getPay_type()+
				"&USER_TYPE="+miLianPayBean.getUser_type()+
				"&BG_URL="+miLianPayBean.getBg_url()+
				"&PAGE_URL="+miLianPayBean.getPage_url()+
				"&USER_ID="+miLianPayBean.getUser_id()+
				"&SIGN="+miLianPayBean.getSign()+
				"&SIGN_TYPE="+miLianPayBean.getSign_type()+
				"&BUS_CODE="+miLianPayBean.getBus_code()+
				"&CCT="+miLianPayBean.getCct()+
				"&GOODS_NAME="+miLianPayBean.getGoods_name()+
				"&GOODS_DESC="+miLianPayBean.getGoods_desc()+
				"&ID_NO="+miLianPayBean.getId_no()+
				"&SETT_ACCT_NO="+miLianPayBean.getSett_acct_no()+
				"&CARD_INST_NAME="+miLianPayBean.getCard_inst_name()+
				"&NAME="+miLianPayBean.getName()+
				"&ADD1="+miLianPayBean.getAdd1()+
				"&PHONE_NO="+miLianPayBean.getPhone_no();

		return param;
	}

	public static String getQuickPayYKParams(MiLianQuickPayBean miLianQuickPayBean , MiLianQuickRegister miLianQuickRegister){
		miLianQuickPayBean.setOrder_time(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));

		RequestMsg requestMsg = new RequestMsg();
		requestMsg.setORDER_ID(miLianQuickPayBean.getOrder_id());                     //订单号
		requestMsg.setTRANS_AMT1(miLianQuickPayBean.getOrder_amt());                 //金额，字符串型
		requestMsg.setTRANS_ATIME(miLianQuickPayBean.getOrder_time());                //订单时间
		requestMsg.setUSER_TYPE(miLianQuickPayBean.getUser_type());                   //用户类型
		requestMsg.setUSER_ID(miLianQuickPayBean.getUser_id());                       //测试商户号
		requestMsg.setSIGN_TYPE(miLianQuickPayBean.getSign_type());                   //签名类型
		requestMsg.setMSG_TYPE(miLianQuickPayBean.getBus_code());                     //业务代码

		SignUtil signUtil = new SignUtil();
		String param ="ORDER_ID="+miLianQuickPayBean.getOrder_id()+
				"&ORDER_AMT="+miLianQuickPayBean.getOrder_amt()+
				"&ORDER_TIME="+miLianQuickPayBean.getOrder_time()+
				"&USER_TYPE="+miLianQuickPayBean.getUser_type()+
				"&PAY_TYPE="+miLianQuickPayBean.getPay_type()+
				"&BG_URL="+miLianQuickPayBean.getBg_url()+
				"&USER_ID="+miLianQuickPayBean.getUser_id()+
				"&SIGN="+signUtil.makeYkSign(requestMsg , miLianQuickRegister)+
				"&SIGN_TYPE="+miLianQuickPayBean.getSign_type()+
				"&BUS_CODE="+miLianQuickPayBean.getBus_code()+
				"&ADD1="+miLianQuickPayBean.getAdd1()+
				"&ADD2="+miLianQuickPayBean.getAdd2()+
				"&ADD3="+miLianQuickPayBean.getAdd3()+
				"&ADD4="+miLianQuickPayBean.getAdd4()+
				"&PHONE_NO="+miLianQuickPayBean.getPhone_no()+
				"&CVN2="+miLianQuickPayBean.getCvn2()+
				"&VALID="+miLianQuickPayBean.getValid();

		return param;
	}

	public static String getPayByOtherParams(MiLianQuickDFBean miLianQuickDFBean , MiLianQuickRegister miLianQuickRegister){
		miLianQuickDFBean.setOrder_time(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));

		RequestMsg requestMsg = new RequestMsg();
		requestMsg.setORDER_ID(miLianQuickDFBean.getOrder_id());                     //订单号
		requestMsg.setTRANS_AMT1(miLianQuickDFBean.getOrder_amt());                 //金额，字符串型
		requestMsg.setTRANS_ATIME(miLianQuickDFBean.getOrder_time());                //订单时间
		requestMsg.setPAY_TYPE(miLianQuickDFBean.getPay_type());                     //支付类型
		requestMsg.setUSER_TYPE(miLianQuickDFBean.getUser_type());                   //用户类型
		requestMsg.setUSER_ID(miLianQuickDFBean.getUser_id());                       //测试商户号
		requestMsg.setSIGN_TYPE(miLianQuickDFBean.getSign_type());                   //签名类型
		requestMsg.setMSG_TYPE(miLianQuickDFBean.getBus_code());                     //业务代码
		requestMsg.setCCT(miLianQuickDFBean.getCct());                               //币种
		requestMsg.setBANK_CODE(miLianQuickDFBean.getBank_code());
		requestMsg.setACCOUNT_NO(miLianQuickDFBean.getAccount_no());
		requestMsg.setACCOUNT_NAME(miLianQuickDFBean.getAccount_name());

		SignUtil signUtil = new SignUtil();
		String param ="ORDER_ID="+miLianQuickDFBean.getOrder_id()+
				"&ORDER_AMT="+miLianQuickDFBean.getOrder_amt()+
				"&ORDER_TIME="+miLianQuickDFBean.getOrder_time()+
				"&PAY_TYPE="+miLianQuickDFBean.getPay_type()+
				"&USER_TYPE="+miLianQuickDFBean.getUser_type()+
				"&USER_ID="+miLianQuickDFBean.getUser_id()+
				"&SIGN="+signUtil.makeSign(requestMsg ,miLianQuickRegister)+
				"&SIGN_TYPE="+miLianQuickDFBean.getSign_type()+
				"&BUS_CODE="+miLianQuickDFBean.getBus_code()+
				"&CCT="+miLianQuickDFBean.getCct()+
				"&BANK_CODE="+miLianQuickDFBean.getBank_code()+
				"&BANK_NAME="+miLianQuickDFBean.getBank_name()+
				"&ACCOUNT_NO="+miLianQuickDFBean.getAccount_no()+
				"&ACCOUNT_NAME="+miLianQuickDFBean.getAccount_name();

		return param;

	}

	/***
	 * 绑卡查询
	 * @return
	 */
	public static String getCardInfoQueryParams(MiLianQuickRegister miLianQuickRegister){
		String    ORDER_ID  =new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());        //Test20170206164539185
		String    USER_TYPE ="02";
		String    USER_ID   =miLianQuickRegister.getMerchantNo();       //商户号
		String    SIGN_TYPE ="03";
		String    ACCT_NO =miLianQuickRegister.getBankCard();

		RequestMsg requestMsg = new RequestMsg();
		requestMsg.setORDER_ID(ORDER_ID);                     //订单号
		requestMsg.setUSER_TYPE(USER_TYPE);                   //用户类型
		requestMsg.setUSER_ID(USER_ID);                       //测试商户号
		requestMsg.setSIGN_TYPE(SIGN_TYPE);                   //签名类型
		requestMsg.setACCT_NO(ACCT_NO);
		SignUtil signUtil = new SignUtil();
		String param ="ORDER_ID="+ORDER_ID+
				"&USER_TYPE="+USER_TYPE+
				"&ACCT_NO="+ACCT_NO+
				"&USER_ID="+USER_ID+
				"&SIGN="+signUtil.makeCardInfoQuerySign(requestMsg ,miLianQuickRegister)+
				"&SIGN_TYPE="+SIGN_TYPE;
		return param;
	}

	/***
	 * 订单查询
	 * @return
	 */
	public static String getQueryOrderParams(MilianOrderQueryBean milianOrderQueryBean ,MiLianQuickRegister miLianQuickRegister){

		RequestMsg requestMsg = new RequestMsg();
		requestMsg.setORDER_ID(milianOrderQueryBean.getORDER_ID());                     //订单号
		requestMsg.setUSER_TYPE(milianOrderQueryBean.getUSER_TYPE());                   //用户类型
		requestMsg.setUSER_ID(milianOrderQueryBean.getUSER_ID());                       //测试商户号
		requestMsg.setSIGN_TYPE(milianOrderQueryBean.getSIGN_TYPE());                   //签名类型
		requestMsg.setORI_ORDER_ID(milianOrderQueryBean.getORG_ORDER_ID());

		SignUtil signUtil = new SignUtil();
		String param ="ORDER_ID="+milianOrderQueryBean.getORDER_ID()+
				"&ORG_ORDER_ID="+milianOrderQueryBean.getORG_ORDER_ID()+
				"&USER_TYPE="+milianOrderQueryBean.getUSER_TYPE()+
				"&USER_ID="+milianOrderQueryBean.getUSER_ID()+
				"&SIGN="+signUtil.makeQueryOrderSign(requestMsg,miLianQuickRegister)+
				"&SIGN_TYPE="+milianOrderQueryBean.getSIGN_TYPE();
		return param;
	}

	/**
	 * 拼接绑卡接口请求参数
	 * @return
	 */
	public static String getBindCardParams(MilianBankCardBean milianBankCardBean,MiLianQuickRegister miLianQuickRegister){
		RequestMsg bean = new RequestMsg();
		bean.setORDER_ID(milianBankCardBean.getOrder_id());
		bean.setUSERTYPE(milianBankCardBean.getUser_type());
		bean.setUSER_TYPE(milianBankCardBean.getUser_type());
		bean.setUSER_ID(milianBankCardBean.getUser_id());
		bean.setSIGN_TYPE(milianBankCardBean.getSign_type());
		bean.setMSG_TYPE(milianBankCardBean.getBus_code());
		bean.setCHECK_TYPE(milianBankCardBean.getCheck_type());
		bean.setID_NO(milianBankCardBean.getId_no());
		bean.setNAME(milianBankCardBean.getName());
		bean.setACCT_NO(milianBankCardBean.getAcct_no());
		bean.setPHONE_NO(milianBankCardBean.getPhone_no());

		SignUtil signUtil = new SignUtil();
		String param  = "ORDER_ID="+milianBankCardBean.getOrder_id()
				+"&USER_TYPE="+milianBankCardBean.getUser_type()
				+"&USER_ID="+milianBankCardBean.getUser_id()
				+"&SIGN="+signUtil.makeBindCardSign(bean ,miLianQuickRegister)
				+"&SIGN_TYPE="+milianBankCardBean.getSign_type()
				+"&BUS_CODE="+milianBankCardBean.getBus_code()
				+"&CHECK_TYPE="+milianBankCardBean.getCheck_type()
				+"&ID_NO="+milianBankCardBean.getId_no()
				+"&NAME="+milianBankCardBean.getName()
				+"&ACCT_NO="+milianBankCardBean.getAcct_no()
				+"&PAGE_URL="+milianBankCardBean.getPage_url()
				+"&PHONE_NO="+milianBankCardBean.getPhone_no();

		return param;
	}

	/***
	 * 商户余额查询
	 * @return
	 */
	public static String getMerchBalanceQueryParams(MiLianQuickRegister miLianQuickRegister){
		String    ORDER_ID  = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());        //Test20170206164539185
		String    USER_TYPE ="02";
		String    USER_ID   = miLianQuickRegister.getMerchantNo();       //商户号
		String    SIGN_TYPE ="03";

		RequestMsg requestMsg = new RequestMsg();
		requestMsg.setORDER_ID(ORDER_ID);                     //订单号
		requestMsg.setUSER_TYPE(USER_TYPE);                   //用户类型
		requestMsg.setUSER_ID(USER_ID);                       //测试商户号
		requestMsg.setSIGN_TYPE(SIGN_TYPE);                   //签名类型
		SignUtil signUtil = new SignUtil();
		String param ="ORDER_ID="+ORDER_ID+
				"&USER_TYPE="+USER_TYPE+
				"&USER_ID="+USER_ID+
				"&SIGN="+signUtil.makeMerchBalanceQuerySign(requestMsg ,miLianQuickRegister)+
				"&SIGN_TYPE="+SIGN_TYPE;
		return param;
	}



}
